package com.cltech.service.sys.impl;

import com.cltech.dto.SysDicDto;
import com.cltech.entity.sys.SysDic;
import com.cltech.frame.base.impl.BaseServiceImpl;
import com.cltech.frame.result.ResponseBean;
import com.cltech.frame.specification.utils.SpecificationSortUtil;
import com.cltech.mapper.SysDicMapper;
import com.cltech.repository.sys.SysDicRepository;
import com.cltech.service.sys.SysDicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
@Transactional
public class SysDicServiceImpl extends BaseServiceImpl<SysDic, String> implements SysDicService {
	private SysDicRepository sysDicRepository;
	@Autowired
	private SysDicMapper sysDicMapper;
	@Autowired
	public void setSysUserRepository(SysDicRepository sysDicRepository) {
		this.sysDicRepository = sysDicRepository;
		super.setBaseRepository(sysDicRepository);
	}
	@Override
	public Page<SysDicDto> findAll(Map<String, String[]> queryParameters, int pageNo, int pageSize) {
		Map<String, Object> map = SpecificationSortUtil.creat(queryParameters, SysDic.class);
		Specification<SysDic> specification = (Specification<SysDic>) map.get(SpecificationSortUtil.SEARCHS);
		Sort sort = (Sort) map.get(SpecificationSortUtil.ORDERS);
		Pageable page = null == sort ? new PageRequest(0, 15) : new PageRequest(0, 15, sort);
		if (null == specification) {
			return sysDicMapper.entityToDto(sysDicRepository.findAll(page));
		} else {
			return sysDicMapper.entityToDto(sysDicRepository.findAll(specification,
					page));
		}
	}

	@Override
	public ResponseBean<SysDic> addOrUpdate(SysDicDto dto) {
		return save(sysDicMapper.dtoToEntity(dto));
	}
}
